<?php

namespace App\Ant\Jobs;

use App\Events\Push;
use App\Models\Label;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class LabelReleaseJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 任务最大尝试次数。
     *
     * @var int
     */
    public $tries = 5;

    /**
     * 任务运行的超时时间。
     *
     * @var int
     */
    public $timeout = 600;

    protected $label_ids;

    /**
     * 创建一个新的任务实例。
     *
     * @return void
     */
    public function __construct(array $label_ids = [])
    {
        $this->label_ids = $label_ids;
    }

    /**
     * 运行任务。
     *
     * @return void
     */
    public function handle(Label $label)
    {
        $label = $label->whereIn('id', $this->label_ids);
        $label->whereIn('id', $this->label_ids)->update(['is_state' => true]);
        $label->get()->each(function (Label $item) {
            $url = 'mayiguan.com';
            $path = '/biaoqian/' . $item->id;
            event(new Push($url, $path));
        });
    }
}